package com.example.demo.cafe.integration;

import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.*;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.stereotype.Component;

@Component
public class IntegrationComponent {

    private final int queueCapacity = 10;
//    @Bean
//    public MessageChannel orders(){
//        return new DirectChannel();
//    }
//
//    @Bean
//    public MessageChannel drinks(){
//        return new PublishSubscribeChannel();
//    }


//    ======= 方法1 begin =======
//    @Bean
//    public PollableChannel hotDrinks(){
//        return new QueueChannel (queueCapacity);
//    }
//    @Bean
//    public PollableChannel coldDrinks() {
//        return new QueueChannel(queueCapacity);
//    }
//    @Bean
//    public SubscribableChannel coldDrinkBarista(){
//        return new DirectChannel();
//    }
//    @Bean
//    public SubscribableChannel hotDrinkBarista(){
//        return new DirectChannel();
//    }
//    @Bean
//    @ServiceActivator(inputChannel = "coldDrinks",poller = @Poller(fixedDelay = "100"))
//    public BridgeHandler coldDrinksBridgeHandler(){
//        BridgeHandler bridgeHandler = new BridgeHandler();
//        bridgeHandler.setOutputChannelName("coldDrinkBarista");
//        return bridgeHandler;
//    }
//    @Bean
//    @ServiceActivator(inputChannel = "hotDrinks",poller = @Poller(fixedDelay = "1000"))
//    public BridgeHandler hotDrinksBridgeHandler(){
//        BridgeHandler bridgeHandler = new BridgeHandler();
//        bridgeHandler.setOutputChannelName("hotDrinkBarista");
//        return bridgeHandler;
//    }
//    ======= 方法1 end =======

//    ======= 方法2 begin =======
//    @Bean
//    public PollableChannel hotDrinks(){
//        return new QueueChannel (queueCapacity);
//    }
//    @Bean
//    public PollableChannel coldDrinks() {
//        return new QueueChannel(queueCapacity);
//    }
//    @Bean
//    @BridgeFrom(value = "coldDrinks", poller = @Poller(fixedDelay = "1000"))
//    public SubscribableChannel coldDrinkBarista() {
//        return new DirectChannel();
//    }
//    @Bean
//    @BridgeFrom(value = "hotDrinks", poller = @Poller(fixedDelay = "1000"))
//    public SubscribableChannel hotDrinkBarista() {
//        return new DirectChannel();
//    }
//    ======= 方法2 end =======

//    ======= 方法3 begin =======
//    @Bean
//    public SubscribableChannel coldDrinkBarista(){
//        return new DirectChannel();
//    }
//    @Bean
//    public SubscribableChannel hotDrinkBarista(){
//        return new DirectChannel();
//    }
    @Bean
    @BridgeTo(value = "coldDrinkBarista", poller = @Poller(fixedDelay = "1000"))
    public PollableChannel coldDrinks() {
        return new QueueChannel(queueCapacity);
    }
    @Bean
    @BridgeTo(value = "hotDrinkBarista", poller = @Poller(fixedDelay = "1000"))
    public PollableChannel hotDrinks() {
        return new QueueChannel(queueCapacity);
    }
//    ======= 方法4 end =======

    @ServiceActivator(inputChannel ="deliveries" )
    public void stdout(Message message){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(message);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");

    }
}
